#include "ObjectPool.h"
#include "Common.h"
#include "ConcurrentAlloc.h"

void alloc1()
{
	for (size_t i = 0; i < 5; i++)
	{
		void* ptr = ConcurrentAlloc(8);
	}
}

void alloc2()
{
	for (size_t i = 0; i < 5; i++)
	{
		void* ptr = ConcurrentAlloc(17);
	}
}

void TLSTest()
{
	std::thread t1(alloc1);
	t1.join();

	std::thread t2(alloc2);
	t2.join();

}

void testConcurrentAlloc1()
{
	void* p1 = ConcurrentAlloc(4);
	void* p2 = ConcurrentAlloc(8);
	void* p3 = ConcurrentAlloc(5);
	void* p4 = ConcurrentAlloc(3);
	void* p5 = ConcurrentAlloc(4);
	void* p6 = ConcurrentAlloc(8);
	void* p7 = ConcurrentAlloc(5);



	ConcurrentFree(p1);
	ConcurrentFree(p2);
	ConcurrentFree(p3);
	ConcurrentFree(p4);
	ConcurrentFree(p5);
	ConcurrentFree(p6);
	ConcurrentFree(p7);
}

void ThreadAlloc1()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; i++)
	{
		void* ptr = ConcurrentAlloc(8);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}

void ThreadAlloc2()
{
	std::vector<void*> v;
	for (size_t i = 0; i < 7; i++)
	{
		void* ptr = ConcurrentAlloc(16);
		v.push_back(ptr);
	}

	for (auto e : v)
	{
		ConcurrentFree(e);
	}
}

void testConcurrentAlloc2()
{
	for (size_t i = 0; i < 1024; i++)
	{
		void* p1 = ConcurrentAlloc(4);
		cout << p1 << endl;

	}
	void* p2 = ConcurrentAlloc(7);
	cout << p2 << endl; 
	void* p3 = ConcurrentAlloc(7);
	cout << p3 << endl;
}

void testMutiThread()
{
	std::thread t1(ThreadAlloc1);
	std::thread t2(ThreadAlloc2);

	t1.join();
	t2.join();
}

void testBigPage()
{
	void* ptr = ConcurrentAlloc(257 * 1024);
	ConcurrentFree(ptr);

	void* ptr1 = ConcurrentAlloc(129 * 1024 * 8);
	ConcurrentFree(ptr1);
}

//int main()
//{
//	//TestObjectPool();
//	//TLSTest();
//	testConcurrentAlloc1();
//	//testConcurrentAlloc2();
//	//testMutiThread();
//
//	testBigPage();
//	return 0;
//}
